package com.med.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.med.entity.MedicineDO;
import com.med.mapper.ShopMapper;
import com.med.service.ShopService;
import com.med.vo.MedicineVO;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    // 定义统一的Redis键前缀
    private static final String REDIS_KEY_PREFIX = "medicine:";
    // 存储所有药品ID的集合键
    private static final String MEDICINE_IDS_KEY = "medicine:ids";

    @Override
    public R<List<MedicineDO>> queryAllShop(MedicineVO medicineVO) {
        List<MedicineDO> medicineDOS;

        // 先检查Redis中是否存在药品ID集合
        Boolean hasIds = redisTemplate.hasKey(MEDICINE_IDS_KEY);
        if (Boolean.TRUE.equals(hasIds)) {
            // 从Redis获取所有药品
            medicineDOS = new ArrayList<>();
            List<Object> ids = redisTemplate.opsForList().range(MEDICINE_IDS_KEY, 0, -1);
            if (ids != null && !ids.isEmpty()) {
                for (Object id : ids) {
                    Map<Object, Object> medicineMap = redisTemplate.opsForHash().entries(REDIS_KEY_PREFIX + id);
                    if (medicineMap != null && !medicineMap.isEmpty()) {
                        MedicineDO medicineDO = mapToMedicineDO(medicineMap);
                        if (medicineDO != null) {
                            medicineDOS.add(medicineDO);
                        }
                    }
                }
            }
        } else {
            // 从数据库查询
            medicineDOS = shopMapper.queryGoods(medicineVO);
            if (medicineDOS != null && !medicineDOS.isEmpty()) {
                // 先清空可能存在的旧数据
                redisTemplate.delete(MEDICINE_IDS_KEY);

                // 存入Redis
                for (MedicineDO medicineDO : medicineDOS) {
                    // 存储药品ID到集合
                    redisTemplate.opsForList().rightPush(MEDICINE_IDS_KEY, medicineDO.getMedicineId());

                    // 存储药品详情到哈希表
                    Map<String, Object> medicineMap = medicineDOToMap(medicineDO);
                    redisTemplate.opsForHash().putAll(
                            REDIS_KEY_PREFIX + medicineDO.getMedicineId(),
                            medicineMap);
                }
            }
        }

        return R.ok(medicineDOS);
    }

    // 将MedicineDO转换为Map用于存储
    private Map<String, Object> medicineDOToMap(MedicineDO medicineDO) {
        Map<String, Object> map = new HashMap<>();
        map.put("medicineId", medicineDO.getMedicineId());
        map.put("medicineName", medicineDO.getMedicineName());
        map.put("medicineCover", medicineDO.getMedicineCover());
        map.put("medicinePrice", medicineDO.getMedicinePrice());
        map.put("medicineNumber", medicineDO.getMedicineNumber());
        map.put("medicineDescription", medicineDO.getMedicineDescription());
        map.put("medicineCategoryId", medicineDO.getMedicineCategoryId());
        map.put("medicineUsage", medicineDO.getMedicineUsage());
        map.put("medicineDosage", medicineDO.getMedicineDosage());
        map.put("medicineNotes", medicineDO.getMedicineNotes());
        map.put("medicineEfficacy", medicineDO.getMedicineEfficacy());
        map.put("medicineCategoryName", medicineDO.getMedicineCategoryName());
        return map;
    }

    // 将Map转换为MedicineDO
    private MedicineDO mapToMedicineDO(Map<Object, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        MedicineDO medicineDO = new MedicineDO();
        medicineDO.setMedicineId((Integer) map.get("medicineId"));  // 根据实际类型调整
        medicineDO.setMedicineName((String) map.get("medicineName"));
        medicineDO.setMedicinePrice((Double) map.get("medicinePrice"));  // 根据实际类型调整
        medicineDO.setMedicineNumber((Integer) map.get("medicineNumber"));  // 根据实际类型调整
        medicineDO.setMedicineDescription((String) map.get("medicineDescription"));
        medicineDO.setMedicineCategoryId((Integer) map.get("medicineCategoryId"));  // 根据实际类型调整
        medicineDO.setMedicineUsage((String) map.get("medicineUsage"));
        medicineDO.setMedicineDosage((String) map.get("medicineDosage"));
        medicineDO.setMedicineNotes((String) map.get("medicineNotes"));
        medicineDO.setMedicineEfficacy((String) map.get("medicineEfficacy"));
        medicineDO.setMedicineCategoryName((String) map.get("medicineCategoryName"));
        medicineDO.setMedicineCover((String) map.get("medicineCover"));


        return medicineDO;
    }
}
